package system

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
	"github.com/flipped-aurora/gin-vue-admin/server/model/system"
	systemReq "github.com/flipped-aurora/gin-vue-admin/server/model/system/request"
	ServiceSystem "github.com/flipped-aurora/gin-vue-admin/server/service/system"
	"github.com/flipped-aurora/gin-vue-admin/server/utils"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type SysPrescriptionApi struct{}

// CreateSysPrescription 创建sysPrescription表
// @Tags SysPrescription
// @Summary 创建sysPrescription表
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data body system.SysPrescription true "创建sysPrescription表"
// @Success 200 {object} response.Response{msg=string} "创建成功"
// @Router /sysPrescription/createSysPrescription [post]
func (sysPrescriptionApi *SysPrescriptionApi) CreateSysPrescription(c *gin.Context) {
	// 创建业务用Context
	ctx := c.Request.Context()

	var sysPrescription system.SysPrescription
	err := c.ShouldBindJSON(&sysPrescription)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	//获取登录用户ID
	sysPrescription.UserId = uint(utils.GetUserID(c))

	err = sysPrescriptionService.CreateSysPrescription(ctx, &sysPrescription)
	if err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("创建成功", c)
}

// DeleteSysPrescription 删除sysPrescription表
// @Tags SysPrescription
// @Summary 删除sysPrescription表
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data body system.SysPrescription true "删除sysPrescription表"
// @Success 200 {object} response.Response{msg=string} "删除成功"
// @Router /sysPrescription/deleteSysPrescription [delete]
func (sysPrescriptionApi *SysPrescriptionApi) DeleteSysPrescription(c *gin.Context) {
	// 创建业务用Context
	ctx := c.Request.Context()

	id := c.Query("id")
	err := sysPrescriptionService.DeleteSysPrescription(ctx, id)
	if err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("删除成功", c)
}

// DeleteSysPrescriptionByIds 批量删除sysPrescription表
// @Tags SysPrescription
// @Summary 批量删除sysPrescription表
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Success 200 {object} response.Response{msg=string} "批量删除成功"
// @Router /sysPrescription/deleteSysPrescriptionByIds [delete]
func (sysPrescriptionApi *SysPrescriptionApi) DeleteSysPrescriptionByIds(c *gin.Context) {
	// 创建业务用Context
	ctx := c.Request.Context()

	ids := c.QueryArray("ids[]")
	err := sysPrescriptionService.DeleteSysPrescriptionByIds(ctx, ids)
	if err != nil {
		global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("批量删除成功", c)
}

// UpdateSysPrescription 更新sysPrescription表
// @Tags SysPrescription
// @Summary 更新sysPrescription表
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data body system.SysPrescription true "更新sysPrescription表"
// @Success 200 {object} response.Response{msg=string} "更新成功"
// @Router /sysPrescription/updateSysPrescription [put]
func (sysPrescriptionApi *SysPrescriptionApi) UpdateSysPrescription(c *gin.Context) {
	// 从ctx获取标准context进行业务行为
	ctx := c.Request.Context()

	var sysPrescription system.SysPrescription
	err := c.ShouldBindJSON(&sysPrescription)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = sysPrescriptionService.UpdateSysPrescription(ctx, sysPrescription)
	if err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("更新成功", c)
}

// FindSysPrescription 用id查询sysPrescription表
// @Tags SysPrescription
// @Summary 用id查询sysPrescription表
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param id query int true "用id查询sysPrescription表"
// @Success 200 {object} response.Response{data=system.SysPrescription,msg=string} "查询成功"
// @Router /sysPrescription/findSysPrescription [get]
func (sysPrescriptionApi *SysPrescriptionApi) FindSysPrescription(c *gin.Context) {
	// 创建业务用Context
	ctx := c.Request.Context()

	id := c.Query("id")
	resysPrescription, err := sysPrescriptionService.GetSysPrescription(ctx, id)
	if err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败:"+err.Error(), c)
		return
	}
	response.OkWithData(resysPrescription, c)
}

// GetSysPrescriptionList 分页获取sysPrescription表列表
// @Tags SysPrescription
// @Summary 分页获取sysPrescription表列表
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data query systemReq.SysPrescriptionSearch true "分页获取sysPrescription表列表"
// @Success 200 {object} response.Response{data=response.PageResult,msg=string} "获取成功"
// @Router /sysPrescription/getSysPrescriptionList [get]
func (sysPrescriptionApi *SysPrescriptionApi) GetSysPrescriptionList(c *gin.Context) {
	// 创建业务用Context
	ctx := c.Request.Context()

	var pageInfo systemReq.SysPrescriptionSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	//获取登录用户ID
	userId := uint(utils.GetUserID(c))

	//获取门店ID
	storeId, err := ServiceSystem.UserServiceApp.GetStoreIdById(userId)
	if err != nil {
		global.GVA_LOG.Error("获取门店id失败!", zap.Error(err))
		response.FailWithMessage("获取门店id失败:"+err.Error(), c)
		return
	}

	list, total, err := sysPrescriptionService.GetSysPrescriptionInfoList(ctx, pageInfo, storeId)
	if err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败:"+err.Error(), c)
		return
	}

	response.OkWithDetailed(response.PageResult{
		List:     list,
		Total:    total,
		Page:     pageInfo.Page,
		PageSize: pageInfo.PageSize,
	}, "获取成功", c)
}

// GetSysPrescriptionPublic 不需要鉴权的sysPrescription表接口
// @Tags SysPrescription
// @Summary 不需要鉴权的sysPrescription表接口
// @Accept application/json
// @Produce application/json
// @Success 200 {object} response.Response{data=object,msg=string} "获取成功"
// @Router /sysPrescription/getSysPrescriptionPublic [get]
func (sysPrescriptionApi *SysPrescriptionApi) GetSysPrescriptionPublic(c *gin.Context) {
	// 创建业务用Context
	ctx := c.Request.Context()

	// 此接口不需要鉴权
	// 示例为返回了一个固定的消息接口，一般本接口用于C端服务，需要自己实现业务逻辑
	sysPrescriptionService.GetSysPrescriptionPublic(ctx)
	response.OkWithDetailed(gin.H{
		"info": "不需要鉴权的sysPrescription表接口信息",
	}, "获取成功", c)
}
